using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Clanta.Web.BusinessLogic;
using NHibernate;
using Clanta.Core.Domain;
using System.Collections.Generic;
using Clanta.Core.DataInterfaces;

namespace Clanta.Web.Controls
{
    public partial class CustomerListWebPart : WebPartUserControl
    {

        #region Web Part Life Cycle

        protected void Page_Init(object sender, EventArgs e)
        {
            CustomerObjectDataSource.Inserting += new ObjectDataSourceMethodEventHandler(CustomerObjectDataSource_Inserting);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                WebPart.Title = "Kunder";
                BindCustomersGridView();
                MultiView1.SetActiveView(CustomerListView);
            }
        }

        #endregion

        #region Event Handlers

        protected void NewCustomerButton_Click(object sender, EventArgs e)
        {
            MultiView1.SetActiveView(NewCustomerView);
            NameTextBox.Focus();
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            //NameTextBox.Text
            CustomerObjectDataSource.Insert();

            //Customer customer = new Customer();

            //ISession session = NHibernateHelper.GetCurrentSession();
            //using (ITransaction tx = session.BeginTransaction())
            //{
            //    session.Save(customer);
            //    tx.Commit();
            //}

            //BindCustomersGridView();
            //// Automatically select the newly created customer
            //SelectCustomer(customer);

            ClearNewCustomerForm();
            MultiView1.SetActiveView(CustomerListView);
        }

        void CustomerObjectDataSource_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            e.InputParameters["Name"] = NameTextBox.Text;
        }

        protected void CancelButton_Click(object sender, EventArgs e)
        {
            ClearNewCustomerForm();
            MultiView1.SetActiveView(CustomerListView);
        }

        //protected void CustomersGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        //{
        //    DataKey dataKey = CustomersGridView.DataKeys[e.RowIndex];
        //    int customerId = (int)dataKey.Value;

        //    Customer customerToDelete = null;

        //    ISession session = NHibernateHelper.GetCurrentSession();
        //    using (ITransaction tx = session.BeginTransaction())
        //    {
        //        customerToDelete = session.Load<Customer>(customerId);
        //        session.Delete(customerToDelete);
        //        tx.Commit();
        //    }

        //    BindCustomersGridView();
        //    // Deselct the selected customer
        //    SelectCustomer(null);
        //    OnCustomerDeleted(new CustomerEventArgs(customerToDelete));
        //}

        protected void CustomersGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            Customer c = null;

            ISession session = NHibernateHelper.GetCurrentSession();
            using (ITransaction tx = session.BeginTransaction())
            {
                c = session.Load<Customer>(CustomersGridView.SelectedDataKey.Value);
                tx.Commit();
            }

            OnCustomerSelected(new CustomerEventArgs(c));
        }

        #endregion

        #region Private Methods

        private void SelectCustomer(Customer customer)
        {
            if (customer == null)
            {
                CustomersGridView.SelectedIndex = -1;
                return;
            }

            // Find index of customer in GridView
            for (int i = 0; i < CustomersGridView.DataKeys.Count; i++)
            {
                if (((int)CustomersGridView.DataKeys[i].Value) == customer.Id)
                {
                    // Select the index
                    CustomersGridView.SelectedIndex = i;
                    break;
                }
            }
            // Fire event that the customer has been selected
            OnCustomerSelected(new CustomerEventArgs(customer));
        }

        private void BindCustomersGridView()
        {
            //List<Customer> customerList = customerList = DaoFactory.CustomerDao.GetCustomersSortedByName();
            //CustomersGridView.DataSource = customerList;
            //CustomersGridView.DataBind();
        }

        private void ClearNewCustomerForm()
        {
            NameTextBox.Text = "";
        }

        #endregion

        #region Events

        public event EventHandler<CustomerEventArgs> CustomerSelected;

        public event EventHandler<CustomerEventArgs> CustomerDeleted;

        protected void OnCustomerSelected(CustomerEventArgs e)
        {
            if (CustomerSelected != null)
            {
                CustomerSelected(this, e);
            }
        }

        protected void OnCustomerDeleted(CustomerEventArgs e)
        {
            if (CustomerDeleted != null)
            {
                CustomerDeleted(this, e);
            }
        }

        public class CustomerEventArgs : EventArgs
        {
            private Customer customer;

            public CustomerEventArgs(Customer customer)
            {
                this.customer = customer;
            }

            public Customer Customer
            {
                get { return customer; }
            }

        }

        #endregion


    }

    [System.ComponentModel.DataObject]
    public class CustomerBLL
    {
        /// <summary>
        /// Exposes accessor for the <see cref="IDaoFactory" /> used by all user controls.
        /// </summary>
        public IDaoFactory DaoFactory
        {
            get
            {
                return (IDaoFactory)CustomHttpApplication.WindsorContainer[typeof(IDaoFactory)];
            }
        }

        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select, true)]
        public List<Customer> GetCustomers(string sortParameter, int maximumRows, int startRowIndex)
        {
            return DaoFactory.CustomerDao.GetCustomersSortedByName();
        }

        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Update, true)]
        public void UpdateCustomer(long id)
        {
        }
        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Insert, true)]
        public void InsertCustomer(string Name)
        {
            Customer customer = new Customer(Name);
            DaoFactory.CustomerDao.Save(customer);
        }

        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Delete, true)]
        public void DeleteCustomer(long original_Id)
        {
        }
    
    }
}